package com.djx.基础;

import org.apache.zookeeper.*;

import org.apache.zookeeper.data.Stat;
import org.junit.Test;


public class ZKSet {

    ZooKeeper zooKeeper = new MyZookepper01().zooKeeper;

    /**
     * 同步修改节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void syncSet01() throws KeeperException, InterruptedException {
        Stat stat = zooKeeper.setData("/djx", "djx值".getBytes(), -1);
        System.out.println("版本号是 : " + stat.getVersion()); //获取版本号
        System.out.println("的值是 : " + new String(zooKeeper.getData("/djx", false, null)));
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    /**
     * 同步修改节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void asyncSet01() throws KeeperException, InterruptedException {
        //arg1 : 节点的路径
        //arg2 : 修改节点的数据
        //arg3 : 数据版本号 -1代表版本号不参与更新
        zooKeeper.setData("/djx", "djxValue".getBytes(), -1, new AsyncCallback.StatCallback() {
            public void processResult(int i, String s, Object o, Stat stat) {
                //i = 0 代表成功
                System.out.println(i);
                //路径
                System.out.println(s);
                //上下文传递对象
                System.out.println(o);
                //属性描述对象
                System.out.println(stat.getCtime());

                //值
                System.out.println(stat.getNumChildren());

            }
        }, "修改成功");
        Thread.sleep(10000);
        System.out.println(new String(zooKeeper.getData("/djx", false, null)));
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }


}
